//
//  CYPaymentAffirmView.m
//  cyjf
//
//  Created by tauCross on 2017/5/16.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYPaymentAffirmView.h"

#import <IQKeyboardManager.h>

#import "CYSMSExpireManager.h"
#import "CYGlobalTimerManager.h"



@interface CYPaymentAffirmView ()
{
    BOOL IQKeyboardEnable;
    BOOL IQKeyboardEnableAutoToolbar;
}

@property(nonatomic, strong)UIView *maskView;
@property(nonatomic, strong)UIView *contentView;

@property(nonatomic, strong)UITextField *smsTextField;
@property(nonatomic, strong)UIButton *smsButton;

@end


@implementation CYPaymentAffirmView

- (instancetype)initWithMobile:(NSString *)mobile
{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if(self)
    {
        [self loadUIWithMobile:mobile];
        [self loadRAC];
        @weakify(self);
        [[CYGlobalTimerManager standardManager] addTarget:self withBlock:^{
            @strongify(self);
            if(self)
            {
                [self checkSeconds];
            }
        }];
        
        [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:CYHideAllCustomViewOnWindowNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
            @strongify(self);
            [self close];
        }];
        [self checkSeconds];
    }
    return self;
}

- (void)loadUIWithMobile:(NSString *)mobile
{
    self.maskView = [[UIView alloc] initWithFrame:self.bounds];
    self.maskView.alpha = 0;
    self.maskView.backgroundColor = HEXCOLORA(@"000000", 0.5);
    [self addSubview:self.maskView];
    
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 295, 218)];
    self.contentView.layer.masksToBounds = YES;
    self.contentView.layer.cornerRadius = 4;
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.center = self.maskView.center;
    self.contentView.centerX = self.width / 2;
    self.contentView.bottom = 0;
    [self addSubview:self.contentView];
    {
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"付款确认";
        titleLabel.textColor = HEXCOLOR(@"444444");
        titleLabel.font = [UIFont systemFontOfSize:18];
        [titleLabel sizeToFit];
        titleLabel.centerX = self.contentView.width / 2;
        titleLabel.centerY = 34;
        [self.contentView addSubview:titleLabel];
        
        UILabel *mobileLabel = [[UILabel alloc] init];
        mobileLabel.text = [@"验证码已发送至手机" stringByAppendingString:[mobile stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"]];
        mobileLabel.font = [UIFont systemFontOfSize:14];
        mobileLabel.textColor = HEXCOLOR(@"666666");
        [mobileLabel sizeToFit];
        mobileLabel.centerX = self.contentView.width / 2;
        mobileLabel.centerY = 75;
        [self.contentView addSubview:mobileLabel];
        
        UIView *smsView = [[UIView alloc] init];
        smsView.width = 275;
        smsView.height = 50;
        smsView.backgroundColor = HEXCOLOR(@"F7F7F7");
        smsView.layer.borderColor = HEXCOLOR(@"ECECEC").CGColor;
        smsView.layer.borderWidth = 1 / SCREEN_SCALE;
        smsView.centerX = self.contentView.width / 2;
        smsView.top = 97;
        [self.contentView addSubview:smsView];
        {
            self.smsTextField = [[TCNoActionTextField alloc] init];
            self.smsTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入短信验证码" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14], NSForegroundColorAttributeName : HEXCOLOR(@"999999")}];
            self.smsTextField.font = [UIFont systemFontOfSize:14];
            self.smsTextField.textColor = HEXCOLOR(@"444444");
            self.smsTextField.keyboardType = UIKeyboardTypeNumberPad;
            self.smsTextField.width = 164;
            self.smsTextField.height = smsView.height;
            self.smsTextField.top = 0;
            self.smsTextField.left = 10;
            [smsView addSubview:self.smsTextField];
            
            UIView *line = [UIView cy_lineWithColor:HEXCOLOR(@"CCCCCC")];
            line.width = 1 / SCREEN_SCALE;
            line.height = 14;
            line.right = self.smsTextField.right + 10;
            line.centerY = smsView.height / 2;
            [smsView addSubview:line];
            
            self.smsButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.smsButton addTarget:self action:@selector(onSMSButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
            [self.smsButton setTitle:@"重新发送" forState:UIControlStateNormal];
            [self.smsButton setTitleColor:HEXCOLOR(@"999999") forState:UIControlStateDisabled];
            [self.smsButton setTitleColor:APP_DEFAULT_COLOR_BLUE forState:UIControlStateNormal];
            self.smsButton.titleLabel.font = [UIFont systemFontOfSize:14];
            self.smsButton.height = smsView.height;
            self.smsButton.width = smsView.width - line.right;
            self.smsButton.top = 0;
            self.smsButton.left = line.right;
            [smsView addSubview:self.smsButton];
        }
        
        UIView *line = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        line.width = self.contentView.width;
        line.height = 1 / SCREEN_SCALE;
        line.left = 0;
        line.bottom = self.contentView.height - 44;
        [self.contentView addSubview:line];
        
        line = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        line.width = 1 / SCREEN_SCALE;
        line.height = 25;
        line.centerY = self.contentView.height - 22;
        line.centerX = self.contentView.width / 2;
        [self.contentView addSubview:line];
        
        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
        cancelButton.tintColor = HEXCOLOR(@"444444");
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
        cancelButton.width = self.contentView.width / 2;
        cancelButton.height = 44;
        cancelButton.left = 0;
        cancelButton.top = self.contentView.height - 44;
        [cancelButton addTarget:self action:@selector(onCancelButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:cancelButton];
        
        UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeSystem];
        submitButton.tintColor = APP_DEFAULT_COLOR_BLUE;
        [submitButton setTitle:@"确认" forState:UIControlStateNormal];
        submitButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        submitButton.frame = cancelButton.frame;
        submitButton.left = cancelButton.right;
        [submitButton addTarget:self action:@selector(onSubmitButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:submitButton];
    }
}

- (void)loadRAC
{
    @weakify(self);
    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *notification) {
        [UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]
                              delay:0
                            options:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]
                         animations:^{
                             @strongify(self);
                             CGFloat height = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
                             self.contentView.centerY = height / 2;
                         }
                         completion:^(BOOL finished) {
                             
                         }];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

#pragma mark - Actions
- (void)close
{
    [self endEditing:YES];
    [IQKeyboardManager sharedManager].enable = IQKeyboardEnable;
    [IQKeyboardManager sharedManager].enableAutoToolbar = IQKeyboardEnableAutoToolbar;
    @weakify(self);
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         @strongify(self);
                         self.maskView.alpha = 0;
                         self.contentView.top = self.height;
                     }
                     completion:^(BOOL finished) {
                         @strongify(self);
                         [self removeFromSuperview];
                     }];
}

- (void)show
{
    IQKeyboardEnable = [IQKeyboardManager sharedManager].enable;
    IQKeyboardEnableAutoToolbar = [IQKeyboardManager sharedManager].enableAutoToolbar;
    [IQKeyboardManager sharedManager].enable = NO;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    @weakify(self);
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         @strongify(self);
                         self.maskView.alpha = 1;
                         self.contentView.centerY = self.height / 2;
                     }
                     completion:^(BOOL finished) {
                         
                     }];

}

- (void)submitAction
{
    if(self.complete)
    {
        self.complete(self.smsTextField.text);
    }
}

#pragma mark - Control Events
- (void)onCancelButtonTouchUpInside
{
    [self close];
}

- (void)onSubmitButtonTouchUpInside
{
    [self submitAction];
}

- (void)onSMSButtonTouchUpInside
{
    if(self.resend)
    {
        self.resend();
    }
}

#pragma mark - Private Method
- (void)checkSeconds
{
    if(self.checkButton)
    {
        self.checkButton(self.smsButton);
    }
}


@end
